home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / chrome / sync.jar / content / bookmark-menu-overlay.js < prev    next >
Text File  |  2008-07-11  |  10KB  |  255 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks Sync.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Jono DiCarlo <jdicarlo@mozilla.com>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. var Cc = Components.classes;
  38. var Ci = Components.interfaces;
  39.  
  40. /* BookmarksEventHandler is defined in browser.js as a singleton object.
  41.  the onPopupShowing: function BM_onPopupShowing(event) method
  42.  is what adds the "Open All In Tabs" to the bottom.
  43.  Override this function to also add a "Share this folder..." or
  44.  "Cancel/Stop sharing this folder..." depending on its status. */
  45.  
  46. /* TODO:
  47.    4. LONGTERM: might be healthier to add an onPopupShowing event handler to
  48.       the bookmark menu to do this, instead of overriding the original
  49.       handler.  ( Do this by using
  50.       bookmarkMenu.addEventListener( "popupshowing", myfunc, false );
  51.       use getElementById to get bookmarkMenu. )
  52.  
  53.    5. LONGTERM: There's a race condition here in that if this override
  54.       gets called before the handler is set up in the first place, it
  55.       won't work.  Might want to set up a timer callback (with timeout 0)
  56.       to ensure this override gets called only after startup is otherwise
  57.       complete.
  58.  
  59.    6. LONGTERM: The guts of the sharing are going to have to be in a
  60.       component, accessed either through XPCom or by using the
  61.       ObserverService to register listeners and pass messages around.
  62. */
  63.  
  64. // Annotation to use for shared bookmark folders, incoming and outgoing:
  65. const INCOMING_SHARED_ANNO = "weave/shared-incoming";
  66. const OUTGOING_SHARED_ANNO = "weave/shared-outgoing";
  67. const INCOMING_SHARE_ROOT_ANNO = "weave/mounted-shares-folder";
  68.  
  69. const UNSHARE_FOLDER_ICON = "chrome://weave/skin/unshare-folder-16x16.png";
  70. const SHARE_FOLDER_ICON = "chrome://weave/skin/shared-folder-16x16.png";
  71. const SHARED_FOLDER_ICON = "chrome://weave/skin/shared-folder-16x16.png";
  72.  
  73.  
  74. var oldOnPopupShowingFunc = BookmarksEventHandler.onPopupShowing;
  75.  
  76. var prefs = Cc["@mozilla.org/preferences-service;1"].
  77.             getService(Ci.nsIPrefService).getBranch( "extensions.weave." );
  78. var log = Log4Moz.Service.getLogger("Share.Menu");
  79.  
  80. function isFolderSharedOutgoing( menuFolder ) {
  81.   let menuFolderId = menuFolder.node.itemId;
  82.   let annotations = PlacesUtils.getAnnotationsForItem( menuFolderId );
  83.   for ( var x in annotations ) {
  84.     if ( annotations[x].name == OUTGOING_SHARED_ANNO ) {
  85.       return ( annotations[x].value != '' );
  86.     }
  87.   }
  88.   return false;
  89. }
  90.  
  91. function isFolderSharedIncoming( menuFolder ) {
  92.   let menuFolderId = menuFolder.node.itemId;
  93.   let annotations = PlacesUtils.getAnnotationsForItem( menuFolderId );
  94.   for ( var x in annotations ) {
  95.     if ( annotations[x].name == INCOMING_SHARED_ANNO ||
  96.        annotations[x].name == INCOMING_SHARE_ROOT_ANNO ) {
  97.       return ( annotations[x].value != '' );
  98.     }
  99.   }
  100.   return false;
  101. }
  102.  
  103. function getUsernameFromSharedFolder( menuFolder ) {
  104.   // TODO this is almost the same code as isFolderSharedOutgoing, refactor!
  105.   let menuFolderId = menuFolder.node.itemId;
  106.   let annotations = PlacesUtils.getAnnotationsForItem( menuFolderId );
  107.   for ( var x in annotations ) {
  108.     if ( annotations[x].name == OUTGOING_SHARED_ANNO ) {
  109.       return ( annotations[x].value );
  110.     }
  111.   }
  112.   return false;
  113. }
  114.  
  115. function adjustBookmarkMenuIcons() {
  116.   let bookmarkMenu = document.getElementById( "bookmarksMenuPopup" );
  117.   let currentChild = bookmarkMenu.firstChild;
  118.   while (currentChild) {
  119.     if (currentChild.localName != "menuitem" && currentChild.node) {
  120.       let label = currentChild.getAttribute( "label" );
  121.       if ( label ) { // a crude way of skipping the separators
  122.     if ( isFolderSharedOutgoing( currentChild ) ) {
  123.       currentChild.setAttribute( "image", SHARED_FOLDER_ICON );
  124.     }
  125.       }
  126.  
  127.     }
  128.     currentChild = currentChild.nextSibling;
  129.   }
  130. }
  131.  
  132. BookmarksEventHandler.onPopupShowing = function BT_onPopupShowing_new(event) {
  133.  
  134.   /* Call the original version, to put all the stuff into the menu that
  135.      we expect to be there: */
  136.   oldOnPopupShowingFunc.call( BookmarksEventHandler, event );
  137.  
  138.   /* Get the global extensions.weave.ui.sharebookmarks preference,
  139.      don't add anything to the menu unless it's turned on! */
  140.   if ( prefs.getBoolPref( "ui.sharebookmarks" ) == false ) {
  141.     return;
  142.   }
  143.  
  144.   /* Try to set the icons of shared folders...
  145.   Problem: this only works on the second, and subsequent, time that the
  146.   bookmark menu pops up.  The first time after firefox starts, it seems that
  147.   the expected bookmark folder items aren't even in the menu yet.*/
  148.   adjustBookmarkMenuIcons();
  149.  
  150.   // Get the menu...
  151.   let target = event.originalTarget;
  152.   let stringBundle = document.getElementById("weaveStringBundle");
  153.  
  154.   /* Don't add the command if this menu is the main bookmark menu;
  155.    Also not if it's one of the magic folders like "recently bookmarked"
  156.    or "bookmarks toolbar" or "recently tagged".  Normal folders only. */
  157.   if ( event.target.parentNode.node == undefined ) {
  158.     return;
  159.   }
  160.   let node = event.target.parentNode.node;
  161.   if ( node.type != node.RESULT_TYPE_FOLDER ) {
  162.     return;
  163.   }
  164.  
  165.   /* Don't add the command if this menu is part of one of the incoming
  166.    * bookmark folder shares!  That would be all kinds of weird recursion
  167.    * that make my head hurt. */
  168.   if (isFolderSharedIncoming( event.target.parentNode)) {
  169.     return;
  170.   }
  171.  
  172.   // put a separator line if there isn't one already.
  173.   /* TODO the separator line moves between the first and second menu view
  174.      of a submenu with only one bookmark in it.  Weird. */
  175.   if (!target._endOptSeparator) {
  176.     // create a separator before options
  177.     target._endOptSeparator = document.createElement("menuseparator");
  178.     target._endOptSeparator.setAttribute("builder", "end");
  179.     target._endMarker = target.childNodes.length;
  180.     target.appendChild(target._endOptSeparator);
  181.   }
  182.  
  183.   function doShareMenuItem( event ) {
  184.     let selectedMenuFolder = event.target.parentNode.parentNode;
  185.     if ( isFolderSharedOutgoing( selectedMenuFolder ) ) {
  186.       // Un-share the selected folder:
  187.       let username = getUsernameFromSharedFolder(selectedMenuFolder);
  188.       dump( "In bookmark-menu-overlay.js: type of selectedMenuFolder is ");
  189.       dump( typeof selectedMenuFolder );
  190.       Weave.Service.shareData("bookmarks",
  191.                   false, // turn share off
  192.                               null, // no callback needed
  193.                               selectedMenuFolder.node.itemId,
  194.                               username);
  195.     } else {
  196.       // Pop the dialog box for sharing the selected folder:
  197.       let type = "Sync:Share";
  198.       let uri = "Chrome://weave/content/share.xul";
  199.       let options = null;
  200.  
  201.       let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
  202.         getService(Ci.nsIWindowMediator);
  203.       let window = wm.getMostRecentWindow(type);
  204.       if (window) {
  205.         window.focus();
  206.       } else {
  207.         var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
  208.           getService(Ci.nsIWindowWatcher);
  209.         if (!options) {
  210.            options = 'chrome,centerscreen,dialog,modal,resizable=yes';
  211.         }
  212.         ww.activeWindow.openDialog(uri, '', options, selectedMenuFolder);
  213.       }
  214.     }
  215.   }
  216.  
  217.   // add an item for "share folder", only if it's not already there
  218.   if ( !target._endOptShareFolder ) {
  219.     target._endOptShareFolder = document.createElement("menuitem");
  220.     /* Set mini-icon on the menu item: */
  221.     target._endOptShareFolder.setAttribute( "class", "menu-iconic" );
  222.     target._endOptShareFolder.addEventListener( "command",
  223.                                                 doShareMenuItem,
  224.                                                 false );
  225.     target.appendChild( target._endOptShareFolder );
  226.   }
  227.  
  228.   /* Grey out the share folder item if we're not logged into weave or
  229.      if weave is disabled: */
  230.   if ( !Weave.Service.enabled || !Weave.Service.isInitialized) {
  231.     target._endOptShareFolder.setAttribute( "disabled", "true" );
  232.     if (!Weave.Service.enabled) {
  233.       dump( "Menu item disabled because weave not enabled.\n");
  234.     }
  235.     if (!Weave.Service.isInitialized){
  236.       dump( "Menu item disabled because weave not logged in.\n");
  237.     }
  238.   }
  239.  
  240.   // Set name and icon of menu item based on shared status:
  241.   let isShared = isFolderSharedOutgoing( event.target.parentNode );
  242.   if ( isShared ) {
  243.     /* If the folder is shared already, the menu item is Un-Share Folder */
  244.     let label = stringBundle.getString("unShareBookmark.menuItem");
  245.     target._endOptShareFolder.setAttribute( "label", label );
  246.     target._endOptShareFolder.setAttribute( "image", UNSHARE_FOLDER_ICON );
  247.   } else {
  248.     /* If the folder is not shared already, the menu item is Share Folder */
  249.     let label = stringBundle.getString("shareBookmark.menuItem");
  250.     target._endOptShareFolder.setAttribute( "label", label );
  251.     target._endOptShareFolder.setAttribute( "image", SHARE_FOLDER_ICON );
  252.   }
  253.  
  254. }
  255.